package com.google.code.cubeirc.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.pircbotx.Colors;
import org.pircbotx.PircBotX;

import com.google.code.cubeirc.base.BaseTab;
import com.google.code.cubeirc.connection.Connection;
import com.google.code.cubeirc.connection.data.GenericChannelDataResponse;
import com.google.code.cubeirc.editor.EditorManager;
import com.google.code.cubeirc.queue.MessageQueue;
import com.google.code.cubeirc.queue.MessageQueueEnum;
import com.google.code.cubeirc.queue.MessageQueueEvent;

public class ListChannelsForm extends BaseTab {
	private Table table;
	private TableColumn tblclmnTopic;
	private TableColumn tblclmnName;
	private TableColumn tblclmnUsers;
	private Button btnFetch;
	private Label lblStatus;
	private final Composite comp;

	public ListChannelsForm(Composite composite, int style, String name) {
		super(composite, style, name);
		comp = composite;
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				tblclmnTopic.setWidth(table.getSize().x - tblclmnUsers.getWidth() - tblclmnName.getWidth());				
			}
		});
		setLayout(new FormLayout());

		Button btnClose = new Button(this, SWT.NONE);
		btnClose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Close();
			}
		});
		btnClose.setImage(SWTResourceManager.getImage(ListChannelsForm.class, "/com/google/code/cubeirc/resources/img_remove.gif"));
		FormData fd_btnClose = new FormData();
		fd_btnClose.right = new FormAttachment(100, -10);
		btnClose.setLayoutData(fd_btnClose);
		btnClose.setText("Close");

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION |  SWT.VIRTUAL);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if (table.getSelectionCount() > 0)
				{
					MessageQueue.addQueue(MessageQueueEnum.CHANNEL_USR_JOIN, table.getSelection()[0].getText(0));
				}
			}
		});
		fd_btnClose.top = new FormAttachment(table, 6);
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(0, 10);
		fd_table.bottom = new FormAttachment(100, -42);
		fd_table.left = new FormAttachment(0, 10);
		fd_table.right = new FormAttachment(100, -10);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		tblclmnName = new TableColumn(table, SWT.NONE);
		tblclmnName.setWidth(103);
		tblclmnName.setText("Name");

		tblclmnUsers = new TableColumn(table, SWT.NONE);
		tblclmnUsers.setWidth(100);
		tblclmnUsers.setText("Users");

		tblclmnTopic = new TableColumn(table, SWT.NONE);
		tblclmnTopic.setWidth(410);
		tblclmnTopic.setText("Topic");

		btnFetch = new Button(this, SWT.NONE);
		btnFetch.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				asyncExec(new Runnable() {

					@Override
					public void run() {
						table.removeAll();
						Connection.getIrcclient().listChannels();
					}
				});
			}
		});
		btnFetch.setImage(SWTResourceManager.getImage(ListChannelsForm.class, "/com/google/code/cubeirc/resources/img_sendfile.png"));
		FormData fd_btnFetch = new FormData();
		fd_btnFetch.top = new FormAttachment(btnClose, 0, SWT.TOP);
		fd_btnFetch.left = new FormAttachment(table, 0, SWT.LEFT);
		btnFetch.setLayoutData(fd_btnFetch);
		btnFetch.setText("Fetch");

		lblStatus = new Label(this, SWT.NONE);
		lblStatus.setAlignment(SWT.CENTER);
		FormData fd_lblStatus = new FormData();
		fd_lblStatus.right = new FormAttachment(btnClose, -6);
		fd_lblStatus.top = new FormAttachment(table, 12);
		fd_lblStatus.left = new FormAttachment(btnFetch, 6);
		lblStatus.setLayoutData(fd_lblStatus);
		lblStatus.setText("...");

		composite.addControlListener(new ControlListener() {

			@Override
			public void controlResized(ControlEvent e) {
				Rectangle area = comp.getClientArea();
				Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
				int width = area.width - 2*table.getBorderWidth();
				if (preferredSize.y > area.height + table.getHeaderHeight()) {
					// Subtract the scrollbar width from the total column width
					// if a vertical scrollbar will be required
					Point vBarSize = table.getVerticalBar().getSize();
					width -= vBarSize.x;
				}
				Point oldSize = table.getSize();
				if (oldSize.x > area.width) {
					// table is getting smaller so make the columns 
					// smaller first and then resize the table to
					// match the client area width
					tblclmnUsers.setWidth(width/3);
					tblclmnTopic.setWidth(width - tblclmnUsers.getWidth());
					table.setSize(area.width, area.height);
				} else {
					// table is getting bigger so make the table 
					// bigger first and then make the columns wider
					// to match the client area width
					table.setSize(area.width, area.height);
					tblclmnUsers.setWidth(width/3);
					tblclmnTopic.setWidth(width - tblclmnUsers.getWidth());

				}
			}

			@Override
			public void controlMoved(ControlEvent e) {
				// TODO Auto-generated method stub

			}
		});


	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	@Override
	public void actionPerformed(MessageQueueEvent e) {

		if (e.getMsgtype() == MessageQueueEnum.CHANNEL_LIST)
		{
			GenericChannelDataResponse gcd = (GenericChannelDataResponse)e.getData();
			addToTable(gcd);

		}
		super.actionPerformed(e);
	}

	private void addToTable(final GenericChannelDataResponse data)
	{
		asyncExec(new Runnable() {

			@Override
			public void run() {
				TableItem ti = new TableItem(table, SWT.NORMAL);
				ti.setText(0, data.getName());
				ti.setText(1, String.format("%s", data.getCount()));
				ti.setText(2, Colors.removeFormattingAndColors(data.getTopic()));
				//TableEditor te = new TableEditor(table);
				//te.grabHorizontal = te.grabVertical = true;
				//StyledText st = new StyledText(table, SWT.SINGLE | SWT.NORMAL | SWT.READ_ONLY);
				//EditorManager.ParseColoredString(data.getTopic(), st);
				//te.setEditor(st, ti, 2);	
				//lblStatus.setText(String.format("Getting info channel %s",data.getName()));
			}
		});

	}
}
